import { defineStore, acceptHMRUpdate } from 'pinia'
import { EventBus } from '@/utils'
import { WebMediaPop } from "../models/web/web-media"
import { getMediaDataToServer } from '@/service/web/user'



export const useWebMediaStore = defineStore('webMedia', {
  state: (): IWebMediaStore => {
    return {
      mediaPop: new WebMediaPop(),
      mediaAllData: {},
    }
  },
  getters: {

  },
  actions: {
    setMediaPop(params: IWebMediaPop | any) {
      this.mediaPop.picAttr = ''
      this.mediaPop.comAttr = ''
      this.mediaPop.event = null
      this.mediaPop.item = {}
      _.merge(this.mediaPop, params)
      this.mediaPop.isActive = true
      EventBus.$emit('refreshMediaPop')
    },
    hiddenMediaPop() {
      this.mediaPop.picAttr = ''
      this.mediaPop.comAttr = ''
      this.mediaPop.isActive = false
      EventBus.$emit('refreshMediaPop')
    },
    getMediaData({ page, pageSize, type, site_id, cb, isResetList }: { page: number, pageSize: number, type: number, site_id?: string | number, cb?: Function, isResetList?: boolean }) {
      const params = { page, pageSize, type }
      if(site_id) {
        params['site_id'] = site_id
      }
      getMediaDataToServer(params, (data: IWebMediaDataItem) => {
        const data_ = {}
        data_[type] = data
        if(isResetList) {
          data_[params.type].isResetList = true
        }
        this.setMediaData(data_)
        if(cb) {
          cb()
        }
      })
    },
    setMediaData(data: IWebMediaData) {
      _.keys(data).forEach((type: number) => {
        const mediaItems: IWebMediaItem[] = (data[type] as IWebMediaDataItem).list
        for (let i = 0; i < mediaItems.length; i++) {
          mediaItems[i].isDelete = false
          mediaItems[i].loading = false
        }
        if (!this.mediaAllData[type]) {
          this.mediaAllData[type] = data[type]
        } else {
          let isResetList: boolean = false
          if (data[type].isResetList) {
            //@ts-ignore
            isResetList = data[type].isResetList
            delete data[type].isResetList
          }
          _.forEach(data[type], (value: any, key: string) => {
            if (key == "list") {
              if (isResetList) {
                this.mediaAllData[type][key] = value
              } else {
                const arr = _.concat(this.mediaAllData[type][key], value)
                this.mediaAllData[type][key] = arr
              }
            } else {
              this.mediaAllData[type][key] = value
            }
          })
        }
      })
    },
    deleteMediaList({type, items}: {type: number, items: string[]}) {
      const arr: any[] = [] //{id:"18848"}
      items.forEach((id: string) => {
        arr.push({ id })
      })
      this.mediaAllData[type].list = _.differenceBy(this.mediaAllData[type].list, arr, 'id')
    },
    addMediaItem({type, item}: {type: number, item: IWebMediaItemLoading | any}) {
      // item: { loading: true, name: file.name }
      if (this.mediaAllData[type]) {
        this.mediaAllData[type].list.unshift(item)
      }
    },
    updateMediaItem(data: any) {
      for (let key in data.item) {
        this.mediaAllData[data.type].list[data.index][key] = data.item[key]
      }
    },
  },

})

if (import.meta.hot) {
  import.meta.hot!.accept(acceptHMRUpdate(useWebMediaStore, import.meta.hot))
}